package com.demo.validate;

import org.springframework.util.StringUtils;

/**
 * @author sdyang
 * @create 2019-01-22 8:50
 **/
public enum FieldDataType {
    String("String", new StringValidate()),
    Number("Number", new NumberValidate()),
    Date("Date", new DateValidate()),
    Mobile("Mobile", new MobileValidate()),
    Email("Email", new EmailValidate()),
    Url("Url", new UrlValidate()),
    PostCode("PostCode", new PostCodeValidate()),
    Phone("Phone", new PhoneValidate()),
    IDCard("IDCard", new IDCardValidate()),
    BankCard("BankCard", new BankCardValidate()),
    Creditcard("Creditcard", new CreditcardValidate()),
    SSC("SocialSecurityCard", new SocialSecurityCardValidate());

    private String value;

    private FieldDataValidate dataValidate;

    FieldDataType(String value, FieldDataValidate dataValidate) {
        this.value = value;
        this.dataValidate = dataValidate;
    }

    public String getValue() {
        return this.value;
    }

    public boolean equals(String value) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        return this.value.equalsIgnoreCase(value.trim()) ? true : false;
    }

    public FieldDataValidate getDataValidate() {
        return dataValidate;
    }

    public void setDataValidate(FieldDataValidate dataValidate) {
        this.dataValidate = dataValidate;
    }

    public static FieldDataType parse(String value) {
        if (StringUtils.isEmpty(value)) {
            throw new RuntimeException(java.lang.String.format("constant[%s] enumeration not define.", value));
        }
        for (FieldDataType fieldDataType : FieldDataType.values()) {
            if (fieldDataType.getValue().equalsIgnoreCase(value.trim())) {
                return fieldDataType;
            }
        }
        throw new RuntimeException(java.lang.String.format("constant[%s] enumeration not define.", value));
    }
}
